var app = getApp();
const cnConvert = require('../../utils/changefont.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
      maskFalg:false,
      curDataType:{},
      curDataDate:{},
      curDataCoffeeMaker:{},
      curDaiLi:{},

      curSearchListIdx:0,
      curDaiLiList: [],
      dateList:[],
      typeList:[],
      coffeeMakerList:[],
      orderList:[],
      loading:true,
      offset:0,
      noData:false,
      days:"",
      orderTotal:0,
      priceTotal:0,
      leagueId:-2,
      // partnerId:-2,
      ldcoffee:0,//联动咖啡id
      isDaili:false,


      pageText :{
        'title':'订单详情',
        'orderName':'商品名称',
        'orderDate':'订单时间',
        'orderNo':'订单编号',
        'orderTotal':'订单合计',
        'saleTotal' :'销售合计',
        'psMyjm':'我的加盟商',
        'proType':'产品类型',
        'psCoffee':'咖啡机',
        'yfh':'已发货',
        'wfh':'未发货',
        'fhsb':'发货失败',
        'mysj':'没有数据'
        
      }
     
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      this.setData({
          days:options.days
      })
      
     
      if(wx.getStorageSync('userDo').type == 1){ 
        this.setData({
          leagueId:-1,
          partnerId:-1,
          isDaili:true
        })
      } 
      if (wx.getStorageSync('userDo').font == 1) { //切换繁体

        var textObj = this.data.pageText
        for (var item in textObj) {
          textObj[item] = cnConvert.traditionalized(textObj[item])
        }
        this.setData({
          pageText: textObj
        })
        wx.setNavigationBarTitle({ title: this.data.pageText.title })
      }

      this.initData();
      this.getOrderList();
      this.getTotal();
   


  },
  
  lower:function(){
      var page = this.data.offset
      page++
      this.setData({
          offset: page
      })
      this.getOrderList();
  },



  getOrderList:function(){
    var that = this;
    var user = wx.getStorageSync('userDo');
    that.setData({
        loading:true
    })
    var userType = wx.getStorageSync('userDo').type;
  
    wx.request({    
        url: app.globalData.api + 'getOrderMsgByUserId',
        data:{
            day         :   that.data.curDataDate.value || 0 ,
            type        :   user.type,
            userId     :    user.userId,
            offset      :   that.data.offset,
            id            :  that.data.curDataCoffeeMaker.id || '',
            saleType :  that.data.curDataType.id ||'' ,
            date: that.data.days,
            leagueId: userType==3?-2: (that.data.curDaiLi.userId || that.data.leagueId),
            font: wx.getStorageSync('userDo').font
            // partnerId: userType == 2 ? -2 : wx.getStorageSync('userDo').userId
        },
        success:function(res){
            if (res.statusCode == 200 ){
                    that.setData({
                        loading: false
                    })
                    if (res.data.length) {
                        var list = that.data.orderList
                        for (var i = 0; i < res.data.length; i++) {
                            list.push(res.data[i])
                        }
                        that.setData({
                            orderList: list,
                            noData: false
                        })
                    } else {
                        that.setData({
                            noData: true
                        })
                    }        
            }                  
        }
    })
  },

  // 获取咖啡机筛选列表
  getCoffeeMakerList: function (duser){
    var that = this;
    var user = {}
    user = duser;

    
    return new Promise((resolve, reject) => {
      wx.request({
        url: app.globalData.api + 'getMakerListByUserIdNoPage',
        data: {
          type: user.type < 0 ? wx.getStorageSync('userDo').type : user.type,
          userId: user.type < 0 ? wx.getStorageSync('userDo').userId : user.userId,
          leagueNull: user.userId != -1 ? 0 : (wx.getStorageSync('userDo').type == 1 ? -1 : 0)
        },
        success: function (res) {
          if (res) {
            resolve(res.data)
          } else {
            reject(res.data)
          }
        }
      })
    })    
  },

//   获取时间筛选列表
  getDateList:function(){
      var that = this;
        wx.request({
          url: app.globalData.api + 'dictList/time_interval/' + wx.getStorageSync('userDo').font,
            success:function(res){
                that.setData({
                    dateList:res.data,
                    curDataDate: res.data[0]
                })
            }
        })
  },


  //获取加盟商筛选列表
 getAgentList:function(){
   var that = this;
   return new Promise((resolve, reject) => {
     wx.request({
       url: app.globalData.api + 'getAgentListByAgentId',
       data: {
         select: 0,
         limit: 300,
         offset: 0,
         parent: wx.getStorageSync('userDo').id,
         type: 2
       },
       success: function (res) {
         if (res.data.length) {
           resolve(res.data)

         } else {
           reject(res.data)
         }
       }
     })
   })
 },
  showType:function(e){
        var idx = e.currentTarget.dataset.idx
        if (this.data.curSearchListIdx == idx){
            this.setData({
                maskFalg: !this.data.maskFalg,
                curSearchListIdx: idx
            })
        }else{
            this.setData({
                maskFalg: true,
                curSearchListIdx: idx
            })
        }
       
  },
  closeMask:function(){
      this.setData({
          maskFalg: false,     
      })
  },
  getItem:function(e){
      var name = e.currentTarget.dataset.item
      var that = this;
      var i = this.data.curSearchListIdx
      if ( i==0 ){
          this.setData({
              curDataType: name
          })
      }else if( i == 1){
          this.setData({
              curDataDate: name
          })
      }
      else if(i == 2){
          this.setData({
              curDataCoffeeMaker: name
          })
      }else{
        this.setData({
          curDaiLi: name
        })
        this.getCoffeeMakerList(name).then(function(res){
          that.setData({
            coffeeMakerList: res,
            curDataCoffeeMaker: res[0],
          })
        })
      }
      this.setData({
          maskFalg:false,
          orderList:[],
          offset:0
      })
      this.getOrderList();
      this.getTotal();
  },
 getTotal:function(){
    var that = this;
    var user = wx.getStorageSync('userDo');
    wx.request({
        url: app.globalData.api + 'coffeeOrderCount',
        data:{
            makerUserId: user.userId,
            userTypee: user.type,
            saleType: that.data.curDataType.id || "",
            time: that.data.curDataDate.value || 0,
            makerId: that.data.curDataCoffeeMaker.id || '',
            date: that.data.days,
            leagueId: that.data.curDaiLi.userId || that.data.leagueId
        },
        success:function(res){
            that.setData({
                orderTotal: res.data.orderCount,
                priceTotal: res.data.orderPay,
            })
        }
    })
 },
  initData:function(){
      this.getDateList();
      var that = this;
      var dataType = [
          {id:'',name:'全部'},
          {id:1,name:'咖啡'},
          {id:2,name:'商品'}
      ]
      

      this.getAgentList()
        .then(function (res) {  //获取加盟商筛选列表
          that.setData({
            curDaiLiList: res,
            curDaiLi: res[0],
          })
          return that.getCoffeeMakerList(that.data.curDaiLi)
        })
        .then(function (res) { //获取咖啡机筛选列表

          that.setData({
            coffeeMakerList: res,
            curDataCoffeeMaker: res[0],
          })

        })




      

     this.setData({
         typeList: dataType,
         curDataType: dataType[0],
        
     })
    

  }


})